package net.lmc.realtime.jtp.dim.function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;

import java.util.HashMap;

public class HbaseDimMapFunction extends RichMapFunction<String,String> {

    private HashMap<String,String> dimMap;

    public HbaseDimMapFunction(HashMap<String, String> dimMap) {
        this.dimMap = dimMap;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
    }

    @Override
    public String map(String value) throws Exception {
        //解析JSON
        JSONObject jsonObject = JSON.parseObject(value);

        //添加Hbase表名
        jsonObject.put("hbase_table_name","dim_" + jsonObject.getString("table_name"));

        //添加列族名称
        jsonObject.put("family_name","info");

        //添加rowKey对应的字段名称
        jsonObject.put("row_key_column",dimMap.get(jsonObject.getString("table_name")));

        //返回json
        return jsonObject.toJSONString();
    }

    @Override
    public void close() throws Exception {
        super.close();
    }
}
